

import java.awt.Color;
import java.awt.Image;
import java.awt.event.KeyEvent;

public class MgPlayer extends MgFighter implements MgCommon{
	private int lastDx=0,lastDy=1;
	private MgWeapon selectedWeapon;
	private MgWeapon[] arsenal;
	private int ammo;
	
	private boolean left;
	private boolean right;
	private boolean up;
	private boolean down;
	private boolean firing;
	
	//Window size
	int width,height;
	
	public MgPlayer(int width, int height, int x, int y,Image originalImg,Image hurtImg,MgWeapon[] arsenal,int ammo){
		super(x,y,originalImg,hurtImg);
		//Test weapon
		this.width = width;
		this.height = height;
		selectedWeapon = new MgWeapon(1,40,7,100,2000,10,Color.orange);
		this.arsenal = arsenal;
		if(arsenal!=null)
			selectedWeapon = arsenal[0];
		this.ammo = ammo;
		movement = 2;
	}
	
	public void move(){
		if(left){
			if(x>img.getWidth(null)/2)
			x-=movement;
		}
		if(right){
			if(x<width-img.getWidth(null)/2)
				x+=movement;
		}
		if(up){
			if(y>img.getHeight(null)/2)
				y-=movement;
		}
		if(down){
			if(y<height-img.getHeight(null)/2)
				y+=movement;
		}
	}
	public void fire(){
		if(firing && selectedWeapon.fire()){
			int dx = 0;
			int dy = 0;
			if(left)
				dx-=1;
			if(right)
				dx+=1;
			if(down)
				dy+=1;
			if(up)
				dy-=1;
			if(dx==0 && dy==0){
				dx = lastDx;
				dy = lastDy;
			}
			projectiles.add(new MgProjectile(x+(img.getWidth(null)/2+2)*dx,y+(img.getHeight(null)/2+2)*dy,dx,dy,selectedWeapon.getBulletSpeed(),selectedWeapon.getDamage(),selectedWeapon.getBulletSize(),selectedWeapon.getBulletColor()));
		}
	}
	public int getX(){
		return x;
	}
	public int getY(){
		return y;
	}
	public Image getImage(){
		return img;
	}
	public void keyPressed(KeyEvent e){
		//Boundary behavior assumes the image's position in code is in middle of picture.
		int key = e.getKeyCode();
		if(key == KeyEvent.VK_D){
			lastDx = 1;
			lastDy = 0;
			if(up)
				lastDy-=1;
			if(down)
				lastDy+=1;
			if (x<width-img.getWidth(null)/2)
				right=true;
		}
		if(key == KeyEvent.VK_A){
			lastDx = -1;
			lastDy = 0;
			if(up)
				lastDy-=1;
			if(down)
				lastDy+=1;
			if (x>img.getWidth(null)/2)
				left=true;
		}
		if(key == KeyEvent.VK_W){
			lastDy = -1;
			lastDx = 0;
			if(left)
				lastDx-=1;
			if(right)
				lastDx+=1;
			if (y>img.getHeight(null)/2)
				up=true;
		}
		if(key == KeyEvent.VK_S){
			lastDy = 1;
			lastDx = 0;
			if(left)
				lastDx-=1;
			if(right)
				lastDx+=1;
			if (y<height-img.getHeight(null)/2)
				down=true;
		}
		if(key == KeyEvent.VK_SPACE){
			if(selectedWeapon!=null && !getWeapon().getReload().isRunning()){
				firing=true;
			}
		}
	}
	public void keyReleased(KeyEvent e){
		int key = e.getKeyCode();
		if(key == KeyEvent.VK_D){
			right=false;
		}
		if(key == KeyEvent.VK_A){
			left=false;
		}
		if(key == KeyEvent.VK_W){
			up=false;
		}
		if(key == KeyEvent.VK_S){
			down=false;
		}
		if(key == KeyEvent.VK_E){
			if(!getWeapon().getReload().isRunning())
				reload();
		}
		if(key == KeyEvent.VK_SPACE){
			firing=false;
		}
	}
	public MgWeapon[] getArsenal(){
		return arsenal;
	}
	public MgWeapon getWeapon(){
		return selectedWeapon;
	}
	public int getAmmo(){
		return ammo;
	}
	public int getClip(){
		return getWeapon().getClip();
	}
	public void obtainAmmo(int obtained){
		ammo += obtained;
	}
	public void reload(){
		ammo += getWeapon().reload(ammo);
	}
}
